<template>
    <el-dialog :visible.sync="visible" top="20px" width="90%" :title="!dataForm.id ? $t('add') : $t('templete.edit')"
               :close-on-click-modal="false" :close-on-press-escape="false">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()"
                 label-width="180px">
            <el-row>
                <template v-for="formItem in dataForm.formItems">
                    <el-col :span="8" style="margin-bottom: 15px;">
                        <label style="font-size: large; font-weight: bolder; margin-left: 50px;">{{formItem.label}}:</label>
                        <el-select :v-model="formItem.type" :value="formItem.type" :disabled="formItem.label === '其他'" size="small"
                                   @change="selectTypeChange($event,formItem)" placeholder="请选择"
                                   style="margin-left: 10px; width: 120px;">
                            <el-option
                                    v-for="item in typeOptions"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value">
                            </el-option>
                        </el-select>
                        <label style="margin-left: 10px;">排序:</label>
                        <el-input-number :max="99" :min="0" size="small" v-model="formItem.sort" style="width: 110px; margin-left: 5px;"></el-input-number>
                        <el-divider direction="horizontal"></el-divider>
                        <template v-for="item in formItem.items">
                            <el-form-item :prop="item.placeholderKey" :label="item.placeholderName+'：'">
<!--                                <el-checkbox :v-model="item.require" :checked="item.require === 1" :name="item.placeholderKey"-->
<!--                                             @change="dataChange($event,item)">必填-->
<!--                                </el-checkbox>-->
                                <label>必填:</label>
                                <el-select :v-model="item.require" :value="item.require" size="small"
                                           @change="selectRequestChange($event,item)" placeholder="请选择"
                                           style="margin-left: 5px; width: 90px;">
                                    <el-option
                                            v-for="item in requestOptions"
                                            :key="item.value"
                                            :label="item.label"
                                            :value="item.value">
                                    </el-option>
                                </el-select>
                                <label style="margin-left: 10px;">类型:</label>
                                <el-select :v-model="item.placeholderTag" :value="item.placeholderTag" size="small"
                                           @change="selectChange($event,item)" placeholder="请选择"
                                           style="margin-left: 5px; width: 100px;">
                                    <el-option
                                            v-for="item in options"
                                            :key="item.value"
                                            :label="item.label"
                                            :value="item.value">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                        </template>
                    </el-col>

                </template>

            </el-row>
        </el-form>
        <template slot="footer">
            <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
            <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
        </template>
    </el-dialog>
</template>

<script>
    import {debounce} from 'lodash'


    export default {
        data() {
            return {
                visible: false,
                placeholderTag: '',
                fileList: [],
                requestOptions:[{
                    label: '隐藏',
                    value: -1,
                }, {
                    label: '非必填',
                    value: 0,
                }, {
                    label: '必填',
                    value: 1,
                }],
                typeOptions: [{
                    label: '请选择',
                    value: 0,
                }, {
                    label: '企业方',
                    value: 1,
                }, {
                    label: '客户方',
                    value: 2,
                }, {
                    label: '担保方',
                    value: 3,
                }, {
                    label: '其它企业',
                    value: 4,
                }],
                options: [{
                    label: '文本',
                    value: 0,
                }, {
                    label: '身份证号',
                    value: 1,
                }, {
                    label: '日期',
                    value: 2,
                }, {
                    label: '手机号码',
                    value: 3,
                }, {
                    label: '数字',
                    value: 4,
                }, {
                    label: '签章',
                    value: 5,
                }, {
                    label: '借款期数',
                    value: 6,
                }, {
                    label: '月利率',
                    value: 7,
                }, {
                    label: '还款方式',
                    value: 8,
                }, {
                    label: '每月还款日',
                    value: 9,
                }, {
                    label: '合同金额',
                    value: 10,
                }],
                dataForm: {
                    id: 0,
                    formItems: [],
                    templeteName: '',
                    templetePath: '',
                    remark: ''
                }
            }
        },
        computed: {
            dataRule() {
                return {
                    templeteName: [
                        {required: true, message: this.$t('validate.required'), trigger: 'blur'}
                    ],
                    unifiedSocialCreditCode: [
                        {required: true, message: this.$t('validate.required'), trigger: 'blur'}
                    ]
                }
            }
        },
        methods: {
            init() {
                this.visible = true
                this.$nextTick(() => {
                    this.$refs['dataForm'].resetFields()
                    Promise.all([]).then(() => {
                        if (this.dataForm.id) {
                            this.getInfo()
                        }
                    })
                })
            },

            // 获取信息
            getInfo() {
                this.$axios.get(`/manager/template/templateFieldsById/${this.dataForm.id}`).then(res => {
                    this.dataForm = {
                        ...this.dataForm,
                        formItems: res.data
                    }
                }).catch(() => {
                })
            },
            // 表单提交
            dataFormSubmitHandle: debounce(function () {
                this.$refs['dataForm'].validate((valid) => {
                    if (!valid) {
                        return false
                    }
                    // this.dataForm.menuIdList = JSON.stringify(this.dataForm.menuIdList)
                    this.$http.json('/manager/template/saveTemplateFields', this.dataForm.formItems).then(res => {
                        this.$message({
                            message: this.$t('prompt.success'),
                            type: 'success',
                            duration: 500,
                            onClose: () => {
                                this.visible = false
                                this.$emit('refreshDataList')
                            }
                        })
                    }).catch(() => {
                    })
                })
            }, 1000, {'leading': true, 'trailing': false}),

            dataChange: function (e, target) {
                console.log(e);
                target.require = e;
            },

            selectChange: function (e, target) {
                console.log(e);
                target.placeholderTag = e;
            },

            selectRequestChange: function (e, target) {
                console.log(e);
                target.require = e;
            },

            selectTypeChange: function (e, target) {
                target.type = e;
            }

        }
    }
</script>

<style scoped>
    .el-form-item{
         margin-bottom: 0px;
    }
    .el-divider--horizontal {
        display: block;
        height: 1px;
        width: 100%;
        margin: 10px 0;
    }

    .el-checkbox {
        color: #606266;
        font-weight: 500;
        font-size: 14px;
        cursor: pointer;
        -webkit-user-select: none;
        -moz-user-select: none;
        -ms-user-select: none;
        user-select: none;
        margin-right: 10px;
    }
</style>
